<template>
  <d2-container>
    <div class="row-ac pb-2">
      <div><el-button size="small" type="warning" @click="addClass" icon="el-icon-plus">添加分类</el-button></div>
      <div class="ml-2"><el-button size="small" type="warning" @click="addUrl" icon="el-icon-plus">添加地址</el-button>
      </div>
    </div>
    <div class="d-f">
      <div class="category-tree">
        <div class="category-tree-title font-s-2 flex-1 row text-white">地址分类列表</div>
        <div class="flex-1 mt-2">
          <el-tree :data="routeclassTree" node-key="id" :default-expand-all="false" :expand-on-click-node="false">
            <div class="row-ac flex-1" slot-scope="{ node, data }">
              <div class="font-s-2 flex-1">{{ data.class_name }}</div>
              <div>
                <i class="el-icon-edit mr-2 cp" @click="() => append(data.id)"></i>
                <i class="el-icon-delete cp" @click="() => remove(data.id)"></i>
              </div>
            </div>
          </el-tree>
        </div>
      </div>
      <div class="flex-1 ml-2">
        <el-table :data="routeurlList" border stripe>
          <el-table-column align="center" type="selection" width="50" />
          <el-table-column align="center" label="序号" type="index" width="50" />
          <el-table-column align="center" label="路由名称" prop="route_name" width="90" />
          <el-table-column label="地址" prop="link" width="160" />
          <el-table-column align="center" label="分类" prop="class_name" width="45" />
          <el-table-column align="center" label="操作" width="130">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
              <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog :visible.sync="dialogClassVisible" width="30%" title="产地">
      <el-form :model="formClass" ref="form" label-width="80px">
        <el-form-item label="名称" prop="class_name">
          <div style="width: 50%;">
            <el-input v-model="formClass.class_name"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="父分类" prop="parent">
          <div style="width: 50%;">
            <el-cascader v-model="formClass.parent" :props="{value:'id', label: 'class_name', checkStrictly: true}"
              :options="routeclassTree" @change="handleChange"></el-cascader>
          </div>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <div style="width: 18%;">
            <el-input v-model="formClass.sort"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogClassVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogUrlVisible" width="30%" title="产地">
      <el-form :model="formUrl" ref="form" label-width="80px">
        <el-form-item label="路由名称" prop="route_name">
          <div style="width: 40%;">
            <el-input v-model="formUrl.route_name"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="地址" prop="link">
          <div style="width: 60%;">
            <el-input v-model="formUrl.link"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="分类" prop="parent">
          <div style="width: 50%;">
            <el-cascader v-model="formUrl.routeclass" :props="{value:'id', label: 'class_name', checkStrictly: true}"
              :options="routeclassTree" @change="handleUrlChange"></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUrlVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitFormUrl">确 定</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
  import {
    routeclassTreeUrl,
    routeclassCreateUrl,
    routeclassDetailUrl,
    routeclassUpdateUrl,
    routeclassDeleteUrl,
    routeurlListUrl,
    routeurlCreateUrl,
    routeurlDetailUrl,
    routeurlUpdateUrl,
    routeurlDeleteUrl,
  } from "@/utils/clientistration"
  export default {
    data() {
      return {
        dialogClassVisible: false,
        dialogUrlVisible: false,
        queryInfo: {
          page: 1
        },
        routeurlList: [],
        routeclassTree: [],
        formClass: {
          class_name: '',
          sort: 1,
          parent: ''
        },
        formUrl: {
          route_name: '',
          link: 1,
          routeclass: ''
        },
        addClassStatu: true,
        getClassId: '',
        addUrlStatu: true,
        getUrlId: ''
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getRouteurlList()
        this.getRouteclassTree()
      },
      getRouteurlList() {
        this.$get(routeurlListUrl, this.queryInfo).then(res => {
          this.routeurlList = res.data.data.list
          this.routeurTotal = res.data.data.total
        })
      },
      getRouteclassTree() {
        this.$get(routeclassTreeUrl).then(res => {
          this.routeclassTree = res.data.data.list
        })
      },
      append(classId) {
        this.dialogClassVisible = true
        this.addClassStatu = false
        this.getClassId = classId
        this.$get(routeclassDetailUrl + classId + '/').then(res => {
          if (res.data.code = 200) {
            this.formClass = {
              class_name: res.data.data.class_name,
              sort: res.data.data.sort,
              parent: res.data.data.parent_id
            }
          }
        })
      },
      remove(classId) {
        this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.$del(routeclassDeleteUrl + classId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getRouteclassTree()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      submitForm() {
        if (this.addClassStatu) {
          this.$post(routeclassCreateUrl, this.formClass).then(res => {
              this.getRouteclassTree()
              this.dialogClassVisible = false
              this.$message.success('添加成功')
              this.formClass = {}
              // 刷新分类列表
              this.$emit('refresh')
            })
            .catch(error => {
              console.log(error)
              this.$message.error('添加失败')
            })
        } else {
          this.$put(routeclassUpdateUrl + this.getClassId + '/', this.formClass).then(res => {
              this.getRouteclassTree()
              this.dialogClassVisible = false
              this.$message.success('修改成功')
              this.formClass = {}
              // 刷新分类列表
              this.$emit('refresh')
            })
            .catch(error => {
              console.log(error)
              this.$message.error('添加失败')
            })
        }
      },
      addClass() {
        this.dialogClassVisible = true
        this.addClassStatu = true
        this.formClass = {}
      },
      addUrl() {
        this.dialogUrlVisible = true
        this.addUrlStatu = true
        this.formUrl = {}
      },
      submitFormUrl() {
        if (this.addUrlStatu) {
          this.$post(routeurlCreateUrl, this.formUrl).then(res => {
              this.getRouteurlList()
              this.dialogUrlVisible = false
              this.$message.success('添加成功')
              this.formUrl = {}
              // 刷新分类列表
              this.$emit('refresh')
            })
            .catch(error => {
              console.log(error)
              this.$message.error('添加失败')
            })
        } else {
          this.$put(routeurlUpdateUrl + this.getUrlId + '/', this.formUrl).then(res => {
              this.getRouteurlList()
              this.dialogUrlVisible = false
              this.$message.success('修改成功')
              this.formUrl = {}
              // 刷新分类列表
              this.$emit('refresh')
            })
            .catch(error => {
              console.log(error)
              this.$message.error('添加失败')
            })
        }
      },
      openDelete(urlId) {
        this.$confirm('此操作将永久删除该路由, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 在确定时执行handleDelete的删除
          this.$del(routeurlDeleteUrl + urlId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getRouteurlList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      handleEdit(urlId) {
        this.addUrlStatu = false
        this.dialogUrlVisible = true
        this.getUrlId = urlId
        this.$get(routeurlDetailUrl + urlId + '/').then(res => {
          if (res.data.code == 200) {
            console.log(res.data.data);
            this.formUrl = {
              route_name: res.data.data.route_name,
              link: res.data.data.link,
              routeclass: res.data.data.routeclass
            }
          }
        })
      },
      handleChange(value) {
        if (value && value.length > 0) {
          this.formClass.parent = value[value.length - 1]; // 更新form.category的值为选中的最后一个值
        }
      },
      handleUrlChange(value) {
        if (value && value.length > 0) {
          this.formUrl.routeclass = value[value.length - 1]; // 更新form.category的值为选中的最后一个值
        }
        console.log(this.formUrl.routeclass);
      }
    }
  }
</script>
<style lang="scss">
  .category-tree-title {
    height: 38px;
    min-height: 38px;
    background-color: #62A8D1;
  }

  .category-tree {
    width: 258px;
  }
</style>